
*set working directory
use "anes1620panel_coded.dta", clear

svyset[pweight=weight], psu(psu) strata(strata)

***** CLPM Models *****

* Key Results in Figure 2, Full Results in Appendix 4

* Model 1 - Ideological Groups (Appendix Table 4A)
svy: sem (valuepol20 <- valuepol16 diffideotherm16 sorting16 issextreme16 ///
	interest16 edu16 age16 income16 church16 female16 white16 black16 south16) ///
	(diffideotherm20 <- valuepol16 diffideotherm16 sorting16 issextreme16 ///
	interest16 edu16 age16 income16 church16 female16 white16 black16 south16), ///
	standardized method(mlmv)

* Model 2 - Parties (Appendix Table 4B)
svy: sem (valuepol20 <- valuepol16 partydifftherm16 sorting16 issextreme16 ///
	interest16 edu16 age16 income16 church16 female16 white16 black16 south16) ///
	(partydifftherm20 <- valuepol16 partydifftherm16 sorting16 issextreme16 ///
	interest16 edu16 age16 income16 church16 female16 white16 black16 south16), ///
	standardized method(mlmv) 

* Model 3 - Presidential Candidates (Appendix Table 4C)
svy: sem (valuepol20 <- valuepol16 diffcandtherm16 sorting16 issextreme16 ///
	interest16 edu16 age16 income16 church16 female16 white16 black16 south16) ///
	(diffcandtherm20 <- valuepol16 diffcandtherm16 sorting16 issextreme16 ///
	interest16 edu16 age16 income16 church16 female16 white16 black16 south16), ///
	standardized method(mlmv) 
	
***** Drop Issue Extremity and Sorting (Appendix 8) *****
* Model 1 - Ideological Groups (Appendix Table 8D)
svy: sem (valuepol20 <- valuepol16 diffideotherm16 ///
	interest16 edu16 age16 income16 church16 female16 white16 black16 south16) ///
	(diffideotherm20 <- valuepol16 diffideotherm16 ///
	interest16 edu16 age16 income16 church16 female16 white16 black16 south16), ///
	standardized method(mlmv)

* Model 2 - Parties (Appendix Table 8E)
svy: sem (valuepol20 <- valuepol16 partydifftherm16 ///
	interest16 edu16 age16 income16 church16 female16 white16 black16 south16) ///
	(partydifftherm20 <- valuepol16 partydifftherm16 ///
	interest16 edu16 age16 income16 church16 female16 white16 black16 south16), ///
	standardized method(mlmv) 
	
* Model 3 - Presidential Candidates (Appendix Table 8F)
svy: sem (valuepol20 <- valuepol16 diffcandtherm16 ///
	interest16 edu16 age16 income16 church16 female16 white16 black16 south16) ///
	(diffcandtherm20 <- valuepol16 diffcandtherm16 ///
	interest16 edu16 age16 income16 church16 female16 white16 black16 south16), ///
	standardized method(mlmv) 


***** Appendix 7: Entropy Balance 2016 to 1992 Covariate Distributions *****
append using "anes_mergedfile_1992to1997_coded.dta", force
replace panel = 1992 if panel != 2016 // panel id

* Gen Conditions to Balance Along
gen condition = 0 if panel == 2016
replace condition = 1 if panel == 1992

* Create merged covariate variables
foreach var in valuepol16 partydifftherm16 diffideotherm16 diffcandtherm16 sorting16 issextreme16 interest16 edu16 age16 income16 church16 female16 white16 black16 south16 valuepol92 partydifftherm92 diffideotherm92 diffcandtherm92 sorting92 issextreme92 interest92 edu92 age92 income92 church92_corrected female92 white92 black92 south92 {
		qui sum `var'
		replace `var' = (`var' - `r(min)') / (`r(max)'-`r(min)')
	} // note: use the corrected church variable, Enders and Lupton miscoded their original church variable, though it ultimately made no difference to their conclusions. 

gen sorting_merged = sorting92 
replace sorting_merged = sorting16 if sorting92 == .
gen issextreme_merged = issextreme92 
replace issextreme_merged = issextreme16 if issextreme92 == .
gen interest_merged = interest92 
replace interest_merged = interest16 if interest92 == .
gen edu_merged = edu92 
replace edu_merged = edu16 if edu92 == .
gen age_merged = age92 
replace age_merged = age16 if age92 == .
gen income_merged = income92 
replace income_merged = income16 if income92 == .
gen church_merged = church92_corrected
replace church_merged = church16 if church92_corrected == .
gen female_merged = female92 
replace female_merged = female16 if female92 == .
gen white_merged = white92 
replace white_merged = white16 if white92 == .
gen black_merged = black92 
replace black_merged = black16 if black92 == .
gen south_merged = south92 
replace south_merged = south16 if south92 == .
replace weight = V960004 if panel == 1992
replace weight = V200011b if panel == 2016

* Create Entropy Balancing Weights 
* To install, enter: ssc install ebalance 
ebalance condition sorting_merged issextreme_merged interest_merged edu_merged age_merged income_merged church_merged female_merged white_merged black_merged south_merged, targets(3) basewt(weight) gen(weight_ebal)

svyset[pweight=weight_ebal], psu(psu) strata(strata)
drop if panel == 1992 // to reanalyze 2016-2020 panel weighted to 1992-1996 panel

* Model 1 - Ideological Groups (Appendix Table 7A)
svy: sem (valuepol20 <- valuepol16 diffideotherm16 sorting16 issextreme16 ///
	interest16 edu16 age16 income16 church16 female16 white16 black16 south16) ///
	(diffideotherm20 <- valuepol16 diffideotherm16 sorting16 issextreme16 ///
	interest16 edu16 age16 income16 church16 female16 white16 black16 south16), ///
	standardized method(mlmv)

* Model 2 - Parties (Appendix Table 7B)
svy: sem (valuepol20 <- valuepol16 partydifftherm16 sorting16 issextreme16 ///
	interest16 edu16 age16 income16 church16 female16 white16 black16 south16) ///
	(partydifftherm20 <- valuepol16 partydifftherm16 sorting16 issextreme16 ///
	interest16 edu16 age16 income16 church16 female16 white16 black16 south16), ///
	standardized method(mlmv) 

* Model 3 - Presidential Candidates (Appendix Table 7C)
svy: sem (valuepol20 <- valuepol16 diffcandtherm16 sorting16 issextreme16 ///
	interest16 edu16 age16 income16 church16 female16 white16 black16 south16) ///
	(diffcandtherm20 <- valuepol16 diffcandtherm16 sorting16 issextreme16 ///
	interest16 edu16 age16 income16 church16 female16 white16 black16 south16), ///
	standardized method(mlmv) 

* NOTE: Top halves of Tables 7A-C are just reproductions of key results from Appendix 4A-C
